import { createRouter, createWebHistory } from 'vue-router'
import Login from "../components/Login.vue"
import Index from "../components/Index.vue"
import Home from "../components/Home.vue"
import { checkToken } from '../api'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      path: '/login',
      name: "login",
      component: Login
    },
    {
      path: "/index",
      name: "index",
      component: Index,
      redirect: "/home",
      children: [
        {
          path: "/home",
          name: "home",
          component: Home
        },
        {
          path: "/merchant",
          name: "merchant",
          component: () => import("../components/Merchant.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/banner",
          name: "banner",
          component: () => import("../components/Banner.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/cate",
          name: "cate",
          component: () => import("../components/Cate.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/article",
          name: "article",
          component: () => import("../components/Article.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/product",
          name: "product",
          component: () => import("../components/Product.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/article/add",
          name: "article/add",
          component: () => import("../components/ArticleAdd.vue")
        },
        {
          path: "/article/edit",
          name: "article/edit",
          component: () => import("../components/ArticleEdit.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/product/add",
          name: "product/add",
          component: () => import("../components/ProductAdd.vue"),
          meta: {
            roles: ["admin"]
          }
        },
        {
          path: "/merchant/add",
          name: "/merchant/add",
          component: () => import("../components/MerchantAdd.vue"),
          meta: {
            roles: ["admin"]
          }
        }
      ]
    },
  ]
})


// 路由前置守卫
router.beforeEach(async (to, from, next) => {
  let token = localStorage.getItem("token")

  // 检验token是否有效
  if (token) {
    console.log(typeof to.meta.roles);
    const ret = await checkToken()
    if (ret.code !== 0) {
      localStorage.removeItem("token")
      next("/login")
    }
  }

  /* if (to.path == "/login") {
    token ? next("/index") : next()
  } else {
    token ? next() : next("/login")
  } */

  to.path == "/login" ? token ? next("/index") : next() : token ? next() : next("/login")

})

export default router
